//
//  RegisterNextViewController.m
//  MarianasTrench
//
//  Created by wangjue on 16/3/26.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "RegisterNextViewController.h"

@interface RegisterNextViewController ()<UITextFieldDelegate>
//用户
@property (nonatomic, retain) UITextField *userField;
@property (nonatomic, retain) UILabel *userLabel;
//密码
@property (nonatomic, retain) UITextField *passwodrdField;
@property (nonatomic, retain) UILabel *passworldLabel;
//注册
@property (nonatomic, retain) UIButton *registerButton;
//返回按钮
@property (nonatomic, retain) UIButton *backButton;
@end

@implementation RegisterNextViewController


-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.86 green:0.85 blue:0.86 alpha:1.00];
    
    //用户
    self.userField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 85, self.view.frame.size.height / 2 - 150, 200, 30)];
    _userField.delegate = self;
    _userField.layer.cornerRadius = 15;
    _userField.layer.borderWidth = 1;
    _userField.layer.borderColor = [UIColor grayColor].CGColor;
    _userField.placeholder = @"请输入用户名";
    _userField.clearsOnBeginEditing = YES;
    _userField.enabled = YES;
    [self.view addSubview:_userField];
    
    self.userLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 150, self.view.frame.size.height / 2 - 150, 60, 30)];
    _userLabel.text = @"用户名:";
    _userLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:_userLabel];
    
    
    
    //密码
    self.passwodrdField = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 85, self.view.frame.size.height / 2 - 100, 200, 30)];
    _passwodrdField.layer.cornerRadius = 15;
    _passwodrdField.layer.borderColor = [UIColor grayColor].CGColor;
    _passwodrdField.layer.borderWidth = 1;
    _passwodrdField.placeholder = @"请输入密码";
    _passwodrdField.delegate = self;
    _passwodrdField.secureTextEntry = YES;
    [self.view addSubview:_passwodrdField];
    
    self.passworldLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 150, self.view.frame.size.height / 2 - 100, 60, 30)];
    _passworldLabel.text = @"  密码:";
    _passworldLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:_passworldLabel];
    
    
    
    
    //注册
    self.registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100, self.view.frame.size.height / 2 - 50, 50, 50);
    _registerButton.layer.cornerRadius = 25;
    _registerButton.layer.borderWidth = 1;
    _registerButton.layer.borderColor = [UIColor grayColor].CGColor;
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [_registerButton addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerButton];
    
    //返回按钮
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100 + 200 - 50, self.view.frame.size.height / 2 - 50, 50, 50);
    _backButton.layer.cornerRadius = 25;
    _backButton.layer.borderColor = [UIColor grayColor].CGColor;
    _backButton.layer.borderWidth = 1;
    [_backButton setTitle:@"返回" forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backButton];

    
    
}


-(void)backAction:(UIButton *)button{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}



-(void)registerAction:(UIButton *)button{
    NSString *str = [NSString stringWithFormat:@"%@", _userField.text];
    const char *cString = [str UTF8String];
    NSInteger num = strlen(cString);
    
    NSString *entryStr = [NSString stringWithFormat:@"%@", _passwodrdField.text];
    const char *cEntryString = [entryStr UTF8String];
    NSInteger entryNum = strlen(cEntryString);
    
    if ((str.length >= 7 && str.length <= 15) && num != 3 && entryNum != 3 && (entryStr.length >= 5 && entryStr.length <= 15)) {
        
        BmobUser *bUser = [[BmobUser alloc] init];
        [bUser setUserName:_userField.text];
        [bUser setPassword:_passwodrdField.text];
        [bUser setObject:@18 forKey:@"age"];
        [bUser signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
            if (isSuccessful){
                NSLog(@"Sign up successfully");
            } else {
                NSLog(@"%@",error);
            }
        }];
        
        
        [BmobUser loginWithUsernameInBackground:_userField.text
                                       password:_passwodrdField.text];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册成功" message:@"可以登录啦" preferredStyle:UIAlertControllerStyleAlert];

        
        
        UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        }];
        
        [alert addAction:alertAction1];
        [self presentViewController:alert animated:YES completion:nil];
        
        
    }else{
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"注册失败" message:@"用户名必须为纯英文，字数必须在7到15之间，密码必须为纯英文，且字数在5到15之间,请重新注册" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *alertAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
        [alert addAction:alertAction1];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
